<template>
  <div class="app-container">
    <div class="filter-container">
      <div v-if="false" class="btnController">
        <el-button class="buttons" @click="handleGetDatas">
          添加
        </el-button>
      </div>
      <div class="formContainer">
        <label class="filterLabel">关键词:</label>
        <el-input v-model="listQuery.keyword" clearable placeholder="关键词" class="filterInput" @keyup.enter.native="handleFilter" />
        <label class="filterLabel">状态:</label>
        <el-select v-model="listQuery.status" class="search-select" clearable placeholder="请选择">
          <el-option label="所有" :value="0" />
          <el-option label="未使用" :value="1" />
          <el-option label="部分使用" :value="2" />
          <el-option label="已使用" :value="3" />
          <el-option label="已过期" :value="9" />
        </el-select>
        <label v-if="false" class="filterLabel">商品ID:</label>
        <el-input v-if="false" v-model="listQuery.goods_id" clearable placeholder="商品ID" class="filterInput" @keyup.enter.native="handleFilter" />
        <label class="filterLabel">景区ID:</label>
        <el-input v-model="listQuery.scenic_id" clearable placeholder="景区ID" class="filterInput" @keyup.enter.native="handleFilter" />
        <el-button class="buttons" @click="handleFilter">
          搜索
        </el-button>
        <el-select v-model="excelType" class="fideselect" style="margin-left: 20px;" placeholder="导出Excel" :loading="downloadLoading" @change="handleExpload">
          <el-option label="当前页面" :value="1" />
          <el-option label="所有页面" :value="2" />
        </el-select>
      </div>
    </div>
    <el-table
      :key="tableKey"
      :data="list"
      style="width: 100%;"
      :header-cell-style="{background:'#ebebeb',height:'#54px'}"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="手机号码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.user_mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.addtime }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="商品ID" width="150px" align="center">
        <template slot-scope="{row}">
          <el-link :href="'?goods_id'+row.goods_id">
            <span>{{ row.goods_id }}</span>
          </el-link>
        </template>
      </el-table-column> -->
      <el-table-column label="景区ID" align="center">
        <template slot-scope="{row}">
          <el-link :href="'?scenic_id'+row.scenic_id">
            <span>{{ row.scenic_id }}</span>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="景区名称" align="center">
        <template slot-scope="{row}">
          <el-link :href="'?scenic_id'+row.scenic_id">
            <span>{{ row.scenic_name }}</span>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.goods_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center">
        <template slot-scope="{row}">
          <span>{{ row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <span><el-tag :type="statusMap[row.status]">{{ row.statusName }}</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column v-show="false" label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="detailShow(row.id)">
            详情
          </el-button>
          <el-popconfirm
            v-if="false"
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="删除后不可以恢复，确定删除吗？"
            @onConfirm="handleDelete(row,$index)"
          >
            <el-button slot="reference" size="mini" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.per_page" @pagination="getList" />
    <!--查看详情-->
    <el-dialog :visible.sync="dialogDetail" title="查看详情">
      <el-form ref="form" :model="rows" label-width="120px">
        <el-form-item label="名称">
          {{ rows.goods_name }}
        </el-form-item>
        <el-form-item label="状态">
          {{ rows.statusName }}
        </el-form-item>
        <el-form-item label="价格">
          {{ rows.price }}
        </el-form-item>
        <el-form-item v-for="(voch,inx) in rows.voucherData" :key="inx" label="信息">
          <el-form-item label="劵码">
            {{ voch.voucher_value }}
          </el-form-item>
          <el-form-item label="有效期">
            {{ voch.start_date }}&nbsp;&nbsp;至&nbsp;&nbsp;{{ voch.end_date }}
          </el-form-item>
          <el-form-item v-for="(cons,ind) in voch.consumeData" :key="ind" label="使用时间">
            {{ cons.ConsumeDate }}
          </el-form-item>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { search, detail } from '@/api/coupons'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { parseTime } from '@/utils/index'
export default {
  name: 'CouponsIndex',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: '未使用',
        2: '部分使用',
        3: '已使用',
        9: '已过期'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      downloadLoading: false,
      excelType: null,
      statusMap: {
        1: 'success',
        2: 'info',
        3: 'warning',
        9: 'danger'
      },
      dialogDetail: false,
      tableKey: 0,
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        per_page: 15,
        keyword: '',
        status: 0,
        type: 2,
        scenic_id: null,
        goods_id: null,
        addtimestart: 0,
        addtimeend: 0
      },
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: { 'normal': '正常', 'hidden': '隐藏' },
      showReviewer: false,
      rows: {
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '更新',
        create: '添加'
      }
    }
  },
  created() {
    this.listQuery.scenic_id = this.$route.query.scenic_id ? this.$route.query.scenic_id : null
    this.listQuery.goods_id = this.$route.query.goods_id ? this.$route.query.goods_id : null
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      var that = this
      const kw = this.listQuery.keyword
      this.listQuery.keyword = kw.trim()
      this.listLoading = true
      search(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          that.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 查看详情
    detailShow(id) {
      detail(id).then(res => {
        this.rows = res.data
        this.dialogDetail = true
      })
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    resetRows() {
      this.rows = {
        groupid: null,
        username: '',
        password: '',
        mobile: '',
        realname: '',
        nickname: 1,
        status: 'normal',
        email: '',
        face: ''
      }
    },
    handleCreate() {
      this.getGroups()
      this.resetRows()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    async handleExpload() {
      console.log(this.excelType)
      this.downloadLoading = true
      const that = this
      let templateList = Object.assign(that.list)
      const loading = this.$loading({
        lock: true,
        text: '加载中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      if (this.excelType === 2) {
        const tempQuery = Object.assign(that.listQuery)
        tempQuery.page = 1
        tempQuery.per_page = this.total
        await search(tempQuery).then((response) => {
          templateList = response.data.data
        })
      }
      loading.close()
      import('@/vendor/Export2Excel').then((excel) => {
        const tHeader = ['序号', '手机号码', '添加时间', '景区ID', '景区名称', '名称', '价格', '状态']
        const filterVal = ['id', 'user_mobile', 'addtime', 'scenic_id', 'scenic_name', 'goods_name', 'amount', 'statusName']
        const data = that.formatJson(filterVal, templateList)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        })
      )
    }
  }
}
</script>
<style scoped >
.fideselect /deep/  .el-input__inner {
  background-color: #66b1ff;
  color: #fff;
}
.fideselect /deep/  .el-input__inner::placeholder {
  color: #fff;
}
</style>
